package com.schoolcloud.files.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.files.config.FileConfig;
import com.schoolcloud.files.dto.ChunkCheckDTO;
import com.schoolcloud.files.dto.FileInfoDTO;
import com.schoolcloud.files.entity.FileMain;
import com.schoolcloud.files.query.FileMainQuery;
import com.schoolcloud.files.service.FileMainService;
import com.schoolcloud.files.service.FileService;
import io.minio.errors.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RequestMapping("/file")
@RestController
@RequiredArgsConstructor
public class FileController {
    private final FileService fileService;
    private final FileMainService fileMainService;
    @GetMapping("/page")
    public R<PageDTO<FileMain>> getFilePage(FileMainQuery query) {
        PageDTO<FileMain> page = fileMainService.getFilePage(query);
        return R.ok(page);
    }
    @GetMapping("/info")
    public R<FileInfoDTO> getFileInfoDTO(@RequestParam String filePath) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        FileInfoDTO fileInfo = fileService.getFileInfo(filePath);
        return R.ok(fileInfo);
    }
    @GetMapping("/check")
    public R<Boolean> checkFileExistByMd5(@RequestParam String md5) {
        return R.ok(fileService.checkFileExist(md5));
    }
    @GetMapping("/chunk/check")
    public R<Boolean> checkChunkExist(ChunkCheckDTO dto) {
        return R.ok(fileService.checkChunkExist(dto));
    }
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Integer id) {
        fileMainService.deleteFileById(id);
        return R.ok();
    }

}
